<?php

namespace app\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Order as OrderModel;

/**
 * Order represents the model behind the search form about `app\models\Order`.
 */
class Order extends OrderModel {
    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['id', 'product_id', 'sale_num', 'status', 'provider_id', 'provider_member_id', 'trader_id', 'trader_member_id'], 'integer'],
            [['unit_price', 'order_price', 'import_duty', 'export_tax'], 'number'],
            [['remark', 'order_time', 'pay_time', 'finsh_time', 'create_at', 'update_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios() {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params) {
        $query = OrderModel::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'product_id' => $this->product_id,
            'unit_price' => $this->unit_price,
            'sale_num' => $this->sale_num,
            'order_price' => $this->order_price,
            'import_duty' => $this->import_duty,
            'export_tax' => $this->export_tax,
            'status' => $this->status,
            'provider_id' => $this->provider_id,
            'provider_member_id' => $this->provider_member_id,
            'trader_id' => $this->trader_id,
            'trader_member_id' => $this->trader_member_id,
            'create_at' => $this->create_at,
            'update_at' => $this->update_at,
        ]);

        $query->andFilterWhere(['like', 'remark', $this->remark])
            ->andFilterWhere(['like', 'order_time', $this->order_time])
            ->andFilterWhere(['like', 'pay_time', $this->pay_time])
            ->andFilterWhere(['like', 'finsh_time', $this->finsh_time]);

        return $dataProvider;
    }
}
